#!/usr/bin/bash
#这是快手自动点击程序
#原理就是利用adb获取布局，来模拟点击小红心
VERSION="2022-01-18"

exit_game()
{
	echo "等待5秒后退出应用"
	sleep 5
	exit 0
}
RST="$(tput sgr0)"
RED="${RST}$(tput setaf 1)"
BRED="${RST}$(tput bold)$(tput setaf 1)"
GREEN="${RST}$(tput setaf 2)"
YELLOW="${RST}$(tput setaf 3)"
BYELLOW="${RST}$(tput bold)$(tput setaf 3)"
BLUE="${RST}$(tput setaf 4)"
CYAN="${RST}$(tput setaf 6)"
BCYAN="${RST}$(tput bold)$(tput setaf 6)"
ICYAN="${RST}$(tput sitm)$(tput setaf 6)"

clear
runadb=$(adb connect localhost:5555 2>&1)
#echo runadb: ${runadb} \n\n
error_info="error:"
refused_info="refused"
if_contains_error_info=$(echo ${runadb} | grep "${error_info}") 
if_contains_refused_info=$(echo ${runadb} | grep "${refused_info}")
if [[ "$if_contains_error_info" != "" ]] ||  [[  "$if_contains_refused_info" != "" ]]
then 
	echo ${RED}\本机未开启网络调试模式,请按下面步骤进行开启：${RST} 
	echo
	echo ${GREEN}\步骤1，将手机设置成为usb调试模式${RST} 
	echo ${GREEN}\方法：百度关键字\"安卓手机系统怎样打开USB调试模式\"${RST} 
	echo ${GREEN}\进入关于手机-\>点击版本号7次，即可打开开发者选项${RST} 
	echo ${GREEN}\进入开发者选项 打开usb 调试 华为机型还应打开“仅充电”模式下充许adb调试${RST} 
	echo
    echo ${GREEN}\步骤2，连接电脑后运行 adb tcpip 5555${RST} 
	echo ${GREEN}\提示 \"restarting in TCP mode port: 5555\" 就表明已经开启网络调试模式${RST} 
	echo
	echo ${BLUE}\到重启手机之前，都可以正常使用本程序 ${RST} 
	echo ${BLUE}\在正常使用中，最小化快手后即可停止本程序 ${RST} 
	exit_game
fi
#启动快手界面
adb -s localhost:5555 shell am start com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity >/dev/null 2>&1 
sleep 2
#当前界面是否是快手推荐朋友
is_we_wanted_activity=0
itouched=0
 
echo 当期版本: ${VERSION}

#检查当前界面是否是快手推荐朋友
check_is_wechat_sport()
{

#currentActivity=$(adb -s localhost:5555 shell dumpsys activity | grep ActivityRecord |head -n 1)
currentActivity=$(adb -s localhost:5555 shell dumpsys activity | grep "mResume" |head -n 2)
key="explore.activity.ExploreFriendActivity"
#key="MyProfileActivity"
if [[ $currentActivity == *$key* ]]
then 	
	#echo is wechat
	is_we_wanted_activity=1
else
	is_we_wanted_activity=0
	
fi
}
#检查当前界面是否是快手推荐朋友
pull_wechat_sport()
{

check_is_wechat_sport
if [[ is_we_wanted_activity -eq 1 ]] 
then  
	echo ${GREEN}\当前窗口为\"快手-\>快手推荐朋友\"${RST}
	#获取当前界面的ui.xml
	adb  -s localhost:5555  shell uiautomator dump /sdcard/ui.xml >/dev/null 2>&1
	#adb  -s localhost:5555  shell uiautomator dump /sdcard/ui.xml
	#sleep 1 
	adb  -s localhost:5555  pull /sdcard/ui.xml  >/dev/null 2>&1
	#adb  -s localhost:5555  pull /sdcard/ui.xml
fi
}
 
readXmlandClick()
{
click_first=1
#echo "The number of parameters is: ${#}"  
for p in "$@"
do
	#echo currenp is ${p}
  if [[ "$p" -eq 0 ]] ; then
	click_first=0
	#echo "detect first"
  fi
done

array_x=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
array_y=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
array_index=0
#/></node></node><node
#sed -n -e   's/c5s/c5s\n\n/g'  -e 's/\/><\/node><\/node><\/node><\/node>/\n\n&/gp' ui.xml | while read line
#OUTFILE=`sed -n -e   's/c5u/c5u\n\n/g'  -e 's/><\/node><\/node><node/\n\n&/gp' ui.xml`
#OUTFILE=`sed -n -e   's/follow_text/\nfollow_text/g'  -e 's/><\/node><node/\n><\/node><node/gp' ui.xml`
#将关注那个按钮进行换行操作，结尾也进行换行操作
OUTFILE=`sed -n -e   's/"关注"/\n"关注"/g'  -e 's/><\/node><node/\n><\/node><node/gp' ui.xml`
while read line
do
    length=`echo $line|awk -F "" '{print NF}'`
    #if [[ ${length} -le 320 ]] && [[ ${length} -ge 290 ]] ; then

	if [[ $line == *"关注"* ]] ; then
		echo "带关注的长度"${length}
		if [[ ${length} -le 380 ]] && [[ ${length} -ge 320 ]] ; then
			#echo ${line}
			v=${line##*bounds=}
			v=$(echo ${v} | sed 's/\]\[/,/g' | sed 's/\]//g' | sed 's/\[//g' | sed 's/\"//g')
			myarray=(${v//\,/ })
			x_point=$(( (${myarray[0]}+${myarray[2]}) /2 + ($RANDOM%20) )) 
			y_point=$(( (${myarray[1]}+${myarray[3]}) /2 + ($RANDOM%15) ))
			#echo ${v} =\> ${x_point} , ${y_point}
			#echo tap ${v} =\> tap ${x_point} , ${y_point}
			#adb shell input tap ${x_point} ${y_point} 
			#sleep 1
			array_x[${array_index}]=${x_point}
			array_y[${array_index}]=${y_point}
			echo ”解析坐标 “array ${array_index} =\> tap ${array_x[${array_index}]} , ${array_y[${array_index}]}
			array_index=$(( ${array_index} +1 )) 
			
		fi
	fi

done  <<EOF
$OUTFILE
EOF

isGoodxml=0
OUTFILE_length=`echo $OUTFILE|awk -F "" '{print NF}'`
if [[ ${OUTFILE_length} -ge 100 ]] ; then
	echo 有效长度${OUTFILE_length}
	isGoodxml=1
fi
rm -f ~/ui.xml
#是否点击过

#echo length = "${array_index}" param @1 param @2
findmyself=0
for(( i=0;i<${array_index};i++ )) do 
	if [[ ${itouched} -eq 0 ]]  ;then
		itouched=1
	fi 
		
	adb -s localhost:5555 shell input tap ${array_x[${i}]} ${array_y[${i}]}
	echo "点击 ${i} =\> 坐标 ${array_x[${i}]} , ${array_y[${i}]}"
 
	#sleep 1
	check_is_wechat_sport 
	if [[ is_we_wanted_activity -eq 0 ]] 
	then 
        if [[ itouched -eq 1 ]] 
        then 
            itouched=2
            adb -s localhost:5555 shell input keyevent BACK
			sleep 1
			dis=$((120*${i}+360))
			echo "dis=$dis" 
			adb  -s localhost:5555 shell input swipe 300 ${dis} 310 200
			sleep 1 
			findmyself=1
			echo "${GREEN}\"滑屏一次\"${RST} i=${i} adb -s localhost:5555 shell input swipe 300 ${dis} 310 200"
			break
        else
            rm -f ~/ui.xml
            echo "当前非${GREEN}\"快手\"${RST}退出点赞程序 "
            exit_game
   
        fi
	fi
done


if [[ findmyself -eq 0 ]]
then 
	#if [[ isGoodxml -eq 1 ]]
	#then 
		adb  -s localhost:5555 shell input swipe 300 1300 310 200
		sleep 1 
		echo "${GREEN}\"滑屏一次\"${RST} "
	#fi
fi

echo "${GREEN}\"退出 readXmlandClick\"${RST} "
check_is_wechat_sport 
if [[ is_we_wanted_activity -eq 0 ]] 
then 
	echo "当前非${GREEN}\"快手\"${RST}退出点赞程序 "
	exit_game
fi
}
check_is_wechat_sport
if [[ is_we_wanted_activity -eq 1 ]]
then 
	for ((m=0; m<=50; m++))
	do		 
		pull_wechat_sport 
		readXmlandClick ${m} 
	done
	echo "${GREEN}\"退出 for \"${RST} "
else
	echo "请先进入${GREEN}\"快手\"${RST}再点击${GREEN}\"快手推荐朋友\"${RST}再运行该程序 "
	exit_game
fi